package com.yssoft.controller.supervise.inspection;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.inspection.TInspStandard;
import com.yssoft.service.supervise.inspection.SuInspectionService;

/**
 * @author 戈超
 * @time 2018/6/11 下午 04:35
 * @  监管检验管理控制器
 */
@Controller
@RequestMapping(value = "/supervise/inspection")
public class SuInspectionController {

	@Resource
	private SuInspectionService suInspectionService;
	
	/**
	 * 跳转监管-检验标准管理
	 * @author 戈超
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "inspectionPage")
    public String toSuEnterprise(Model model){
		return "supervise/inspection/inspStandardList";
	}
	
	/**
	 * 查询监管-检验标准列表
	 * @author 戈超
	 * @param page
	 * @param limit
	 * @param code
	 * @param name
	 * @param productClassifyId
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "queryInspectionList")
    public JSONObject queryEnterpriseList(@RequestParam(name = "page",defaultValue = "0") int page,
    		@RequestParam(name = "limit",defaultValue = "10") int limit, String code, String name,
    		Integer productClassifyId){
    	JSONObject pageList = suInspectionService.queryInspectionPage(page, limit, code, name, productClassifyId);
    	return pageList;
    }
	
	/**
	 * 跳转监管-检验标准新增
	 * @author 戈超
	 * @return
	 */
	@RequestMapping(value = "inspectAdd")
	public String inspectAdd(){
		return "supervise/inspection/inspStandardForm";
	}
	
	/**
	 * 跳转监管-产品类型选择
	 * @author 戈超
	 * @return
	 */
	@RequestMapping(value = "selectProductClassify")
	public String selectProductClassify(){
		return "supervise/inspection/productClassifySelect";
	}
	
	/**
	 * 跳转监管-产品类型选择2
	 * @author 戈超
	 * @return
	 */
	@RequestMapping(value = "selectProductClassify2")
	public String selectProductClassify2(){
		return "supervise/inspection/productClassifySelect2";
	}
	
	/**
	 * 查询监管-产品类型列表
	 * @param page
	 * @param limit
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "queryClassifyList")
	public JSONObject queryClassifyList(@RequestParam(name = "page",defaultValue = "0") int page,
    		@RequestParam(name = "limit",defaultValue = "5") int limit, String name){
		JSONObject pageList = suInspectionService.queryClassifyList(page, limit, name);
		return pageList;
	}
	
	/**
	 * 新增监管-检验标准新增保存
	 * @author 戈超
	 * @param tInspStandard
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "inspectAddSave")
	public String inspectAddSave(TInspStandard tInspStandard, RedirectAttributes redirectAttributes){
		try {
			suInspectionService.saveInspStandard(tInspStandard, "");
			redirectAttributes.addFlashAttribute("message", "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "保存失败!");
		}
		return "redirect:inspectionPage";
	}
	
	/**
	 * 跳转监管-检验管理编辑
	 * @author 戈超
	 * @return
	 */
	@RequestMapping(value = "inspectionEdit")
	public String inspectionEdit(Integer inspectionId, Model model){
		TInspStandard inspStandard = suInspectionService.queryInspectionById(inspectionId);
		model.addAttribute("inspStandard", inspStandard);
		return "supervise/inspection/inspStandardEdit";
	}
	
	/**
	 * 编辑监管-检验标准编辑保存
	 * @author 戈超
	 * @param tInspStandard
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "inspectEditSave")
	public String inspectEditSave(TInspStandard tInspStandard, String delIds, RedirectAttributes redirectAttributes){
		try {
			suInspectionService.saveInspStandard(tInspStandard,delIds);
			redirectAttributes.addFlashAttribute("message", "编辑成功!");
		} catch (Exception e) {
			e.printStackTrace();
			redirectAttributes.addFlashAttribute("message", "编辑失败!");
		}
		return "redirect:inspectionPage";
	}
	
	/**
	 * 跳转监管-检验管理查看
	 * @author 戈超
	 * @return
	 */
	@RequestMapping(value = "queryInspectView")
	public String queryInspectView(Integer inspectionId, Model model){
		TInspStandard inspStandard = suInspectionService.queryInspectionById(inspectionId);
		model.addAttribute("inspStandard", inspStandard);
		return "supervise/inspection/inspStandardView";
	}
	
	/**
	 * 删除监管-检验管理删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "delInspect")
	public String delProcess(String ids){
		String result = "";
		String[] processIdArray = ids.split(",");
		try {
            for (int i = 0; i < processIdArray.length; i++) {
            	suInspectionService.delProcess(processIdArray[i]);
            }
            result = "success";
        } catch (Exception e) {
            e.printStackTrace();
            result = "error";
        }
		return result;
	}
}
